package com.hjm.mall.api;

import com.hjm.mall.api.param.CommentParam;
import com.hjm.mall.api.vo.CommentVO;
import com.hjm.mall.config.annotation.TokenToMallUserId;
import com.hjm.mall.service.impl.CommentServiceImpl;
import com.hjm.mall.util.Result;
import com.hjm.mall.util.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@ResponseBody
@RequestMapping("/api/comment")
@Api(value = "comment",tags = "哈基米商城——评论接口")
public class CommentController {
    @Autowired
    CommentServiceImpl commentService;

    @GetMapping("/goods")
    @ApiOperation(value = "商品评论", notes = "不需要token验证")
    public Result<CommentVO> getGoodsComment(@ApiParam("商品id")@RequestParam("id")int id){
        return ResultGenerator.genSuccessResult(commentService.getGoodsComment(id));
    }

    @GetMapping("/store")
    @ApiOperation(value = "店铺评论", notes = "不需要token验证")
    public Result<CommentVO> getStoreComment(@ApiParam("店铺id")@RequestParam("id")int id){
        return ResultGenerator.genSuccessResult(commentService.getStoreComment(id));
    }

    @PostMapping("/new")
    @ApiOperation(value = "新增评论", notes = "不需要token验证")
    public Result<CommentVO> newStoreComment(
            @TokenToMallUserId
            @ApiParam(hidden = true,name = "用户id")
                    Long userId,
            @RequestBody
            @ApiParam("地址信息")
            CommentParam commentParam){
        boolean res = commentService.insertComment(userId,commentParam);
        if (res)
            return ResultGenerator.genSuccessResult("评论成功");
        return ResultGenerator.genFailResult("评论失败");
    }
}
